package com.buddy.learn.algorithm.simple;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author Buddy
 * @date 2021/5/13
 */
public class _求字符串最不重复长字串 {

    /**
     * 求取不含重复字符的最长字串长度
     */
    public static void main(String[] args) {
//        String s = "abcabcbb";
//        String s = "bbbbb";
//        String s = "peekew";
        String s = "";
        int len = lengthOfLongestSubString(s);
        System.out.println(len);
    }

    private static int lengthOfLongestSubString(String s) {

        if (null == s || s.length()==0){
            return 0;
        }
        char[] chars = s.toCharArray();
        int len = chars.length;
        Queue<Character> queue = new ArrayBlockingQueue<>(len);
        int res = 0;
        for (char c : chars) {
            if (queue.contains(c)) {
                res = res > queue.size() ? res : queue.size();
                char curr = queue.remove();
                while (curr != c) {
                    curr = queue.remove();
                }
            }
            queue.add(c);
        }
        return res > queue.size() ? res : queue.size();
    }


}
